
// ipc通信
export default function useRenderIpc() {
  // packageEnv 之后要改成配置
  const packageEnv: any = 'dev'
  const browserIpc = window?.ipc

  const parseObject = (msg: any[]) => {
    return msg.map((item: any) => {
      return typeof item === 'object' ? JSON.parse(JSON.stringify(item)) : item
    })
  } 

  const ipcReceive = (channel: any, callback: (...rest: any[]) => void) => {
    console[packageEnv === 'web' ? 'error' : 'log'](':::[ipcReceive]', channel)
    if (packageEnv === 'web') {
      return
    }
    return browserIpc?.ipcReceive(channel, callback)
  }

  const ipcSend = (channel: any, ...msg: any) => {
    console[packageEnv === 'web' ? 'error' : 'log'](':::[ipcSend]', channel, ...parseObject(msg))
    if (packageEnv === 'web') {
      return
    }
    browserIpc.ipcSend(channel, ...parseObject(msg))
  }


  const ipcInvoke = (channel: any, ...msg: any) => {
    console[packageEnv === 'web' ? 'error' : 'log'](':::[ipcInvoke]', channel, ...parseObject(msg))
    if (packageEnv === 'web') {
      return
    }
    return browserIpc.ipcInvoke(channel, ...parseObject(msg))
  }

  return {
    ipcReceive,
    ipcSend,
    ipcInvoke
  }
}